Yes. It is nice to check these cases out in advance before you start writing the program.
Here is a start to the program.
Notice how the program matches the flowchart,
especially how the while
statement is
nested in the true branch of the if
statement.
The indenting (and the braces {}
) show this structure.
Another thing to notice is that the integer variables are of type long
.
This is because the values are expected to get very large, and long
can
hold larger integers than int
.
import java.util.Scanner; // User enters integer N. // The program calculates N factorial. // class Factorial { public static void main (String[] args ) { Scanner scan = new Scanner( System.in ); long N, fact = 1; System.out.print( "Enter N: " ); N = scan.nextLong(); if ( ) { while ( ) { ; ; } System.out.println( "factorial is " + fact ); } else { System.out.println("N must be zero or greater"); } } }
Fill in the four blanks to complete the program. Here are some phrases you might use:
fact = fact*N
N > 1
N = N - 1
N >= 0